package sandbox.vaadin.skiller.entity;

import com.vaadin.data.Item;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class SkillgroupEditor extends Window implements Button.ClickListener
{
	private static final long serialVersionUID = 1L;

//	private final Item skillgroupItem;

	private FieldGroup fieldgroup;

	private Button btnSave;
	private Button btnCancel;

	public SkillgroupEditor(Item skillgroupItem)
	{
//		this.skillgroupItem = skillgroupItem;

		TextField tfId = new TextField("Id");
		TextField tfName = new TextField("Name");

		FormLayout formLayout = new FormLayout();
		formLayout.addComponent(tfId);
		formLayout.addComponent(tfName);

		btnSave = new Button("Save", this);
		btnCancel = new Button("Cancel", this);

		HorizontalLayout horizontalLayout = new HorizontalLayout();
		horizontalLayout.addComponent(btnSave);
		horizontalLayout.addComponent(btnCancel);

		VerticalLayout verticalLayout = new VerticalLayout();
		verticalLayout.addComponent(formLayout);
		verticalLayout.addComponent(horizontalLayout);

		fieldgroup = new FieldGroup(skillgroupItem);
		fieldgroup.setBuffered(true);
		fieldgroup.bind(tfId, "id");
		fieldgroup.bind(tfName, "name");

		setCaption("skillgroup editor");
		setContent(verticalLayout);
	}

	@Override
	public void buttonClick(ClickEvent event)
	{
		if (event.getButton() == btnSave)
		{
			try
			{
				fieldgroup.commit();
//				fireEvent(new EditorSavedEvent(this, skillgroupItem));
			}
			catch (CommitException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (event.getButton() == btnCancel)
		{
			fieldgroup.discard();
		}
		close();
	}

//	public void addListener(EditorSavedListener listener)
//	{
//		try
//		{
//			Method method =
//			    EditorSavedListener.class.getDeclaredMethod("editorSaved",
//			        new Class[] { EditorSavedEvent.class });
//			addListener(EditorSavedEvent.class, listener, method);
//		}
//		catch (final java.lang.NoSuchMethodException e)
//		{
//			// This should never happen
//			throw new java.lang.RuntimeException(
//			    "Internal error, editor saved method not found");
//		}
//	}
//
//	public void removeListener(EditorSavedListener listener)
//	{
//		removeListener(EditorSavedEvent.class, listener);
//	}
//
//	public static class EditorSavedEvent
//	    extends Component.Event
//	{
//    private static final long serialVersionUID = 1L;
//
//    private Item savedItem;
//
//		public EditorSavedEvent(Component source, Item savedItem)
//		{
//			super(source);
//			this.savedItem = savedItem;
//		}
//
//		public Item getSavedItem()
//		{
//			return savedItem;
//		}
//	}
//
//	public interface EditorSavedListener extends Serializable
//	{
//		public void editorSaved(EditorSavedEvent event);
//	}
}